package it.amicosmanettone.finance.stocky;

import it.amicosmanettone.finance.stocky.util.FinanceUtil;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

public class SettingsActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	SharedPreferences prefs;

	Preference preference;

	PreferenceScreen root;

	ListPreference listLanguage;
	ListPreference listCurrency;
	ListPreference listUpdate;
	ListPreference listConversion;
	ListPreference listTrend;

	String valueOfLanguage;
	String valueOfCurrency;
	String valueOfUpdate;
	String valueOfConversion;
	String valueOfTrend;

	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.prefs = PreferenceManager.getDefaultSharedPreferences(this);
		this.prefs.registerOnSharedPreferenceChangeListener(this);

		setupActionBar();

		addPreferencesFromResource(R.xml.preferences);
		this.root = this.getPreferenceScreen();

		this.listLanguage = (ListPreference) root
				.findPreference("pref_list_language");
		this.listCurrency = (ListPreference) root
				.findPreference("pref_list_currency");
		this.listUpdate = (ListPreference) root
				.findPreference("pref_list_update");
		this.listConversion = (ListPreference) root
				.findPreference("pref_list_conversion");
		this.listTrend = (ListPreference) root
				.findPreference("pref_list_trend");

		this.listLanguage.setEntries(R.array.listLanguage);
		this.listLanguage.setEntryValues(R.array.listLanguageValues);

		this.listCurrency.setEntries(R.array.listCurrency);
		this.listCurrency.setEntryValues(R.array.listCurrencyValues);

		this.listUpdate.setEntries(R.array.listUpdate);
		this.listUpdate.setEntryValues(R.array.listUpdateValues);

		this.listConversion.setEntries(R.array.listConversion);
		this.listConversion.setEntryValues(R.array.listConversionValues);

		this.listTrend.setEntries(R.array.listTrend);
		this.listTrend.setEntryValues(R.array.listTrendValues);

		this.valueOfLanguage = this.listLanguage.getValue();
		this.valueOfCurrency = this.listCurrency.getValue();
		this.valueOfUpdate = this.listUpdate.getValue();
		this.valueOfConversion = this.listConversion.getValue();
		this.valueOfTrend = this.listTrend.getValue();

		this.listLanguage.setSummary(this.valueOfLanguage);
		this.listCurrency.setSummary(this.valueOfCurrency);
		this.listUpdate.setSummary(this.valueOfUpdate);
		this.listConversion.setSummary(this.valueOfConversion);
		this.listTrend.setSummary(this.valueOfTrend);

		this.listLanguage.setPersistent(true);
		this.listCurrency.setPersistent(true);
		this.listUpdate.setPersistent(true);
		this.listConversion.setPersistent(true);
		this.listTrend.setPersistent(true);

	}

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	private void setupActionBar() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:

			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {

		this.preference = findPreference(key);

		if (this.preference instanceof ListPreference) {

			this.listLanguage.setSummary(this.listLanguage.getValue());
			this.listCurrency.setSummary(this.listCurrency.getValue());
			this.listUpdate.setSummary(this.listUpdate.getValue());
			this.listConversion.setSummary(this.listConversion.getValue());
			this.listTrend.setSummary(this.listTrend.getValue());
			refresh();

		}
		
	}
	
	public void refresh(){
		
		Editor EDIT = this.prefs.edit();
		
		String defaultLanguage = getText(R.string.prefLanguageEnglish).toString();
		String languageItalian = getText(R.string.prefLanguageItalian).toString();
		
		if(FinanceUtil.IS_LITE_APP_VERSION && this.listLanguage.getValue().equals(languageItalian)){
			
			this.listLanguage.setValue(defaultLanguage);
			this.listLanguage.setSummary(defaultLanguage);
			errorLanguageNoPremium();
			
		}
		else if((FinanceUtil.IS_LITE_APP_VERSION == false) && this.listLanguage.getValue().equals(defaultLanguage)){
			
			EDIT.remove("start_app_language").commit();
			okChangeLanguage();
			
		}
		else if((FinanceUtil.IS_LITE_APP_VERSION == false) && this.listLanguage.getValue().equals(languageItalian)){
			
			EDIT.putString("start_app_language", "ITALIAN").commit();
			okChangeLanguage();
			
		}
		
	}
	
	public void errorLanguageNoPremium() {

		String textNoPremium = getText(R.string.prefErrorLanguage)
				.toString();
		String OKButton = getText(R.string.mainNewPortfolioOKButton).toString();

		new AlertDialog.Builder(this)
				.setMessage(textNoPremium)
				.setCancelable(false)
				.setNeutralButton(OKButton,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int whichButton) {
								
								

							}
						}).show();

	}
	
	public void okChangeLanguage() {

		String textRestart = getText(R.string.prefLanguageRestartText)
				.toString();
		String OKButton = getText(R.string.prefLanguageRestartButton).toString();
		
		final Intent restart = new Intent(this, SplashActivity.class);

		new AlertDialog.Builder(this)
				.setMessage(textRestart)
				.setCancelable(false)
				.setNeutralButton(OKButton,
						new DialogInterface.OnClickListener() {

							public void onClick(DialogInterface dialog,
									int whichButton) {
								
							restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
							startActivity(restart);

							}
						}).show();

	}
	

}
